﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 编辑标题文本命令。在不混淆的情况下，可以用来编辑Comment和Relation的备注文本。
    /// ★①，修改类名。
    /// </summary>
    public static class EditTitleTextCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static EditTitleTextCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            //routedUICmd = new RoutedUICommand("EditTitleTextCommand", "EditTitleTextCommand", typeof(EditTitleTextCommand),//创建RoutedUICommand对象
            //    new InputGestureCollection() 
            //    { 
            //        //★⑤，修改此处两个参数，以便挂接快捷键。
            //        new KeyGesture(Key.Enter,ModifierKeys.None,"Enter")
            //    });

            routedUICmd = new RoutedUICommand(
                "EditTitleTextCommand",
                "EditTitleTextCommand",
                typeof(EditTitleTextCommand),//创建RoutedUICommand对象
                new InputGestureCollection()
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.Enter,ModifierKeys.None,"Enter"),
                });//编辑文本还是加上快捷键更合节奏。

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            if (Globals.SelectedTitlesList.Count > 0)
            {
                EditTitleTextOfSwitchCommentVisibleWhenFullScreen();
            }
            else
            {
                if (Globals.MainWindow.SelectedTitleImage != null)
                {
                    Globals.MainWindow.SelectedTitleImage.LoadImageFromFile(); return;
                }

                if (Globals.MainWindow.SelectedComment != null)
                {
                    EditCommentTextCommand.EditComment(); return;
                }

                if (Globals.MainWindow.SelectedRelation != null)
                {
                    EditCommentTextCommand.EditRelationText(); return;
                }
            }
        }

        /// <summary>
        /// 编辑文本。
        /// </summary>
        /// <param name="initializeText">此参数不为空时，将直接以此参数初始化编辑区的文本。</param>
        public static void EditTitleTextOfSwitchCommentVisibleWhenFullScreen(string initializeText = null)
        {
            try
            {
                MainWindow mainWindow = Globals.MainWindow;

                //全屏时，Enter键用以显示|隐藏标题中的填空项文本。
                if (mainWindow.tbtnFullScreen.IsChecked == true)
                {
                    if (mainWindow.MainSelectedTitle != null)
                    {
                        mainWindow.MainSelectedTitle.IsFillBlankVisible = !mainWindow.MainSelectedTitle.IsFillBlankVisible;
                        mainWindow.RefreshTitleAndLinesLocation();
                        return;
                    }
                }


                Dialogs.TitleTextEditor tte = new Dialogs.TitleTextEditor(
                    Globals.MainWindow.SelectedTitlesList, initializeText);

                AniScrollViewer sv = Globals.MainWindow.mainScrollViewer;

                tte.WindowStartupLocation = WindowStartupLocation.Manual;
                tte.Width = sv.ActualWidth - 54;
                Point ptBase = new Point(0, 0);
                Point ptStart = sv.PointToScreen(ptBase);
                tte.Left = ptStart.X + (sv.ActualWidth - Math.Min(tte.MaxWidth, tte.Width)) / 2;
                tte.Height = 120;
                tte.Top = sv.ActualHeight + ptStart.Y - 130;
                tte.Owner = Globals.MainWindow;

                Title.TryToShowTitleInVisualArea(Globals.MainWindow.MainSelectedTitle, true);

                tte.ShowDialog();

                Globals.MainWindow.RefreshTitleAndLinesLocation();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            if (Globals.MainWindow.SelectedTitlesList.Count > 0)
            {
                e.CanExecute = true;//有可编辑的标题时可用。
            }
            else
            {
                if (Globals.MainWindow.SelectedTitleImage != null ||
                    Globals.MainWindow.SelectedRelation != null ||
                    Globals.MainWindow.SelectedComment != null)
                {
                    e.CanExecute = true;
                }
                else
                {
                    e.CanExecute = false;
                }
            }
        }
    }
}
